package com.example.controller.before;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.entity.Address;
import com.example.entity.Goods;
import com.example.entity.Order;
import com.example.entity.vo.OrderVo;
import com.example.service.AddressService;
import com.example.service.GoodsService;
import com.example.service.OrderService;
import com.example.utils.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@Api(tags = "小程序状态控制器") //swagger的注释
@CrossOrigin
@RequestMapping("/TypeCheck")
@RestController
public class AuditBefore {

    @Autowired
    private GoodsService goodsService;

    @Autowired
    private OrderService orderService;

    @Autowired
    private AddressService addressService;

    @ApiOperation(value = "查询申请中物品")
    @GetMapping(value = "getAuditGoods/{userId}")
    public R getAuditGoods(@PathVariable String userId) {
        if (!StringUtils.isEmpty(userId)) {
            QueryWrapper<Goods> wrapper = new QueryWrapper<>();
            wrapper.eq("is_exhibit", 0);
            wrapper.eq("user_id", userId);
            List<Goods> list = goodsService.list(wrapper);
            return R.ok().data("goods", list);
        }
        return R.error().message("userId为空");
    }

    @ApiOperation(value = "查询已上架物品")
    @GetMapping(value = "publishGoods/{userId}")
    public R publishGoods(@PathVariable String userId) {
        if (!StringUtils.isEmpty(userId)) {
            QueryWrapper<Goods> wrapper = new QueryWrapper<>();
            wrapper.eq("is_exhibit", 1);
            wrapper.eq("is_sell", 0);
            wrapper.eq("user_id", userId);
            List<Goods> list = goodsService.list(wrapper);
            return R.ok().data("goods", list);
        }
        return R.error().message("userId为空");
    }

    @ApiOperation(value = "查询进行中订单")
    @GetMapping(value = "getOrdering/{userId}")
    public R getOrdering(@PathVariable String userId) {
        if (!StringUtils.isEmpty(userId)) {
            QueryWrapper<Order> wrapper = new QueryWrapper<>();
            wrapper.eq("order_type", 0);
            wrapper.eq("user_id_from", userId).or().eq("user_id_to", userId);
            List<Order> list = orderService.list(wrapper);
            List<OrderVo> list2 = new ArrayList();
            for (int i = 0; i < list.size(); i++) {
                OrderVo orderVo = new OrderVo();
                String goodsId = list.get(i).getGoodsId();
                String addIdFrom = list.get(i).getAddIdFrom();
                String addIdTo = list.get(i).getAddIdTo();
                Goods goods = goodsService.getById(goodsId);
                Address addFrom = addressService.getById(addIdFrom);
                Address addTo = addressService.getById(addIdTo);
                orderVo.setGoods(goods);
                orderVo.setAddIdFrom(addFrom);
                orderVo.setAddIdTo(addTo);
                orderVo.setUserIdFrom(list.get(i).getUserIdFrom());
                orderVo.setUserIdTo(list.get(i).getUserIdTo());
                orderVo.setOrderId(list.get(i).getOrderId());
                orderVo.setOrderType(list.get(i).getOrderType());
                orderVo.setExpressId(list.get(i).getExpressId());
                orderVo.setGmtCreate(list.get(i).getGmtCreate());
                list2.add(orderVo);
            }
            return R.ok().data("order", list2);
        }
        return R.error().message("userId为空");
    }

    @ApiOperation(value = "查询全部订单")
    @GetMapping(value = "getOrderList/{userId}")
    public R getOrderList(@PathVariable String userId) {
        if (!StringUtils.isEmpty(userId)) {
            QueryWrapper<Order> wrapper = new QueryWrapper<>();
            wrapper.eq("user_id_from", userId).or().eq("user_id_to", userId);
            List<Order> list = orderService.list(wrapper);
            List<OrderVo> list2 = new ArrayList();
            for (int i = 0; i < list.size(); i++) {
                OrderVo orderVo = new OrderVo();
                String goodsId = list.get(i).getGoodsId();
                Goods goods = goodsService.getById(goodsId);
                orderVo.setGoods(goods);
                orderVo.setUserIdFrom(list.get(i).getUserIdFrom());
                orderVo.setUserIdTo(list.get(i).getUserIdTo());
                orderVo.setOrderId(list.get(i).getOrderId());
                orderVo.setOrderType(list.get(i).getOrderType());
                orderVo.setExpressId(list.get(i).getExpressId());
                orderVo.setGmtCreate(list.get(i).getGmtCreate());
                list2.add(orderVo);
            }
            return R.ok().data("order", list2);
        }
        return R.error().message("userId为空");
    }

    @ApiOperation(value = "查询已完成订单")
    @GetMapping(value = "getEndOrder/{userId}")
    public R getEndOrder(@PathVariable String userId) {
        if (!StringUtils.isEmpty(userId)) {
            QueryWrapper<Order> wrapper = new QueryWrapper<>();
            wrapper.eq("order_type", 1);
            wrapper.eq("user_id_from", userId);
            List<Order> list = orderService.list(wrapper);
            List<OrderVo> list2 = new ArrayList();
            for (int i = 0; i < list.size(); i++) {
                OrderVo orderVo = new OrderVo();
                String goodsId = list.get(i).getGoodsId();
                Goods goods = goodsService.getById(goodsId);
                orderVo.setGoods(goods);
                orderVo.setUserIdFrom(list.get(i).getUserIdFrom());
                orderVo.setUserIdTo(list.get(i).getUserIdTo());
                orderVo.setOrderId(list.get(i).getOrderId());
                orderVo.setOrderType(list.get(i).getOrderType());
                orderVo.setExpressId(list.get(i).getExpressId());
                orderVo.setGmtCreate(list.get(i).getGmtCreate());
                list2.add(orderVo);
            }
            return R.ok().data("order", list2);
        }
        return R.error().message("userId为空");
    }
}
